
import BulletCtr from "../BulletCtr";
import { bulletManager } from "../BulletManager";
import FishCtr from "../FishCtr";
import { fishManager } from "../FishManager";
import { player } from "../playerCtr";
import QuadtreeCollision from "./QuadtreeCollision";

export default class ColliderObjManager {

    //所有子弹的collider都存在这里 
    all_bullet_collider: Map<number, cc.Collider> = new Map();

    //所有鱼的collider都存在这里
    all_fish_collider: Map<number, cc.Collider> = new Map();

    //四叉树碰撞检测器
    _quadCollision: QuadtreeCollision;

    checkFlage: number = 0

    check() {
        let self = this
        //每一帧 都把每一颗子弹拿出来和鱼做碰撞检测
        this.all_bullet_collider.forEach((collider: cc.Collider, id: number) => {
            const element = collider;
            if (element == null || element == undefined) {
                self.all_bullet_collider.delete(id)
                console.log('子弹的碰撞有问题   ', element)
            } else {
                if (element.isValid) {
                    const contacts = this._quadCollision.check(this.all_fish_collider, element);
                    if (contacts.length > 0) {
                        self.bulletBomb(element, contacts[0])
                        self.all_bullet_collider.delete(id)
                    }
                } else {
                    console.log('子弹的碰撞有问题 isValid')
                    self.all_bullet_collider.delete(id)
                }
            }
        })
    }

    /**
     * 子弹碰撞 爆炸
     * @param bullet 碰撞的子弹
     * @param fish 碰撞的鱼
     */
    async bulletBomb(bullets: cc.Collider, fish: cc.Collider) {
        let fishInfo = fish.node.getComponent(FishCtr)
        let position = bullets.node.position


        //把子弹移除掉
        bulletManager.putBullet(bullets.node)
        let bomb = await bulletManager.getBomb('prefab/bomb')
        bomb.position = position
        let anim = bomb.getComponent(cc.Animation)
        anim.once('finished', function () {
            bulletManager.putBulletBomb(bomb)
        }, this);
        anim.play()
        bomb.parent = cc.find('Canvas/game/effectContent')

        //随机一个概率 宰了鱼
        let killNumber = Math.random() * 10
        if (killNumber > 5) {//一半的几率弄死它
            this.romoveFishCollider(fishInfo.id)
            fishManager.putFish(fish.node)
            //爆金币  金币飞过去 
            let coin = await bulletManager.getCoin('prefab/coin')
            coin.parent = cc.find('Canvas/game/effectContent')
            coin.position = position
            coin.setScale(1)
            let c_anim = coin.getComponent(cc.Animation)
            c_anim.once('finished', function () {
                //动画播完就飞过去 之后再回收
                let position = cc.find('Canvas/game/playerContent/coin').position
                let action = cc.moveTo(0.2, cc.v2(position.x, position.y))
                let sequence = cc.sequence(action, cc.callFunc(() => {
                    //飞过去之后给自己加分
                    player.coin+=1
                    bulletManager.putCoin(coin)
                }))
                coin.runAction(sequence)

            }, this);
            c_anim.play()
        }
    }

    setBulletCollider(bullet: cc.Node) {
        if (bullet && bullet.isValid) {
            let collider = bullet.getComponent(cc.Collider)
            this.all_bullet_collider.set(Number(bullet.getComponent(BulletCtr).bulletInfo.id), collider)
        }
    }

    setFishCollider(fish: cc.Node) {
        if (fish && fish.isValid) {
            let collider = fish.getComponent(cc.Collider)
            let fishinfo = fish.getComponent(FishCtr)
            let id = fishinfo.id
            this.all_fish_collider.set(id, collider)
        }
    }

    romoveBulletCollider(id: number) {
        this.all_bullet_collider.delete(id)
    }

    romoveFishCollider(id: number) {
        this.all_fish_collider.delete(id)
        // console.log('鱼被回收时 ',index)
    }

    clearCollider() {
        this.all_fish_collider.clear()
        this.all_bullet_collider.clear()
    }

    init(node) {
        this._quadCollision = new QuadtreeCollision(node.getBoundingBoxToWorld());
    }

}
export const colliderObjManager = new ColliderObjManager()

